﻿using Store.Manager.ERP.Application.Dtos;
using Store.Manager.ERP.Common.Models;
using Store.Manager.ERP.Domain.Entities;
using Store.Manager.ERP.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Store.Manager.ERP.Application.Services
{
    public class IncomingOrderService : IIncomingOrderService
    {
        private IIncomingOrderRepository _repository;
        private IOrganizationRepository _organizationRepository;
        public IncomingOrderService(IIncomingOrderRepository repository, IOrganizationRepository organizationRepository) {
            _repository = repository;
            _organizationRepository = organizationRepository;
        }
        public async Task<CommonResponse> Add(IncomingOrderDto incomingOrderDto)
        {
            var org = await _organizationRepository.GetByIdAsync(incomingOrderDto.OrganizationCode);
            if (org != null)
            {
                IncomingOrder incomingOrder = new IncomingOrder()
                {
                    OrderNumber = incomingOrderDto.OrderNumber,
                    OrganizationCode = incomingOrderDto.OrganizationCode,
                    SupplierCode = incomingOrderDto.SupplierCode,
                    IncomingDate = DateTime.Now
                };
                await _repository.Add(incomingOrder);
                return CommonResponse.OK();
            }
            else
            {
                return CommonResponse.Error("无效的组织编码");
            }
        }
    }
}
